package com.photoeditor.demo.model.image.emoji;

import android.app.Activity;


/**
 * 
 * Emoji的公用类
 *
 */
public class StickerManager {

	//用于加载Emoji的相关信息
	private StickerSyncHandler mEmojiLoader;

	//是否加载Emoji完成
	private boolean mLoadEmojiFinish = false;

	private Activity mActivity;

	private IEmojiLoadListener mOuterListener;

	private IEmojiLoadListener mOwnListener = new IEmojiLoadListener() {
		@Override
		public void onLoadEmojiFinish() {
			if(mActivity != null && !mActivity.isFinishing()) {
				mActivity.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						setLoadEmojiFinish(true);
						if (mOuterListener != null) {
							mOuterListener.onLoadEmojiFinish();
						}
					}
				});
			}
		}
	};


	public StickerManager(Activity context){
		mActivity = context;
		mEmojiLoader = new StickerSyncHandler();
		mEmojiLoader.addListener(mOwnListener);
		initEmojiData();
	}

	public void initEmojiData(){
		mLoadEmojiFinish = false;
		mEmojiLoader.startLoadEmojiApkDetailInfo();
	}

	/**
	 * 刷新Emoji的数据
	 */
	public void refreshEmojiData(){
		mLoadEmojiFinish = false;
		mEmojiLoader.refreshEmojiApkDetailInfo();
	}

	/**
	 * 释放内存
	 */
	public synchronized void destory(){
		mActivity = null;
		mEmojiLoader.destory();
		LocalStickerData.getInstance().recycle();
	}

	//不释放图片
	public synchronized void destroyNotBitmap(){
		mActivity = null;
		mEmojiLoader.destory();
		LocalStickerData.getInstance().recycleNotBitmap();
	}

	public boolean getLoadEmojiFinish(){
		return mLoadEmojiFinish;
	}

	public void setLoadEmojiFinish(boolean loadEmojiFinish){
		mLoadEmojiFinish = loadEmojiFinish;
	}

	public void setEmojiLoadListener(IEmojiLoadListener listener){
		mOuterListener = listener;
	}
}
